package com.gwd.task.controller;

import com.gwd.task.group.GroupUpdate;
import com.gwd.task.model.CodeSubmitRecordDTO;
import com.gwd.task.model.PageUtils;
import com.gwd.task.model.TestDTO;
import com.gwd.task.services.TestServer;
import org.apache.poi.hssf.usermodel.HSSFWorkbook;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletResponse;
import java.io.*;
import java.net.URLEncoder;
import java.text.SimpleDateFormat;
import java.util.Date;

/**
 * @author zhiyongming
 * @date 2020/12/29 9:57
 */
@RestController
@RequestMapping(value = "/v1/test")
public class TestController {
    @Resource
    private TestServer testServer;

    @GetMapping(value = "findByPage")
    public PageUtils findByPage(@Validated TestDTO testDTO, PageUtils page) {
        return testServer.findByPage(testDTO, page);
    }

    @PostMapping(value = "export")
    public void export(@RequestBody @Validated TestDTO testDTO, HttpServletResponse response) {
        String fileName = "BUG汇总-" + new SimpleDateFormat("yyyy-MM-dd").format(new Date()) +".xls";
        HSSFWorkbook wb = testServer.export(testDTO);
        try{ //写入浏览器
            response.setCharacterEncoding("UTF-8");
            response.setHeader("Access-Control-Expose-Headers", "Content-Disposition");
            response.setHeader("Content-Type", "text/html;charset=utf-8");
            response.setHeader("content-disposition", "attachment;filename=" + URLEncoder.encode(fileName, "UTF-8"));
            OutputStream outputStream = response.getOutputStream();
            wb.write(outputStream);
            outputStream.flush();
            outputStream.close();
        }catch(Exception e){
            e.printStackTrace();
        }
    }


    @PostMapping(value = "/add")
    public void add(@Validated @RequestBody TestDTO testDTO, @RequestAttribute("userId") Long id) {
        testServer.save(testDTO, id);
    }

    @PutMapping(value = "/edit")
    public void edit(@Validated @RequestBody TestDTO testDTO, @RequestAttribute("userId") Long id) {
        testServer.save(testDTO, id);
    }

    /**
     * 自测上传图片
     */
    @PutMapping(value = "/self")
    public void self(@Validated({GroupUpdate.class}) @RequestBody TestDTO testDTO, @RequestAttribute("userId") Long userId) {
        testServer.self(testDTO, userId);
    }

    @PutMapping(value = "/reject/{id}")
    public void reject(@PathVariable(value = "id")Long testId, @RequestAttribute("userId") Long userId) {
        testServer.refuse(testId, userId);
    }

    @PutMapping(value = "/open/{id}")
    public void open(@PathVariable(value = "id")Long testId, @RequestAttribute("userId") Long userId) {
        testServer.open(testId, userId);
    }

    @PutMapping(value = "/reopen/{id}")
    public void reOpen(@PathVariable(value = "id")Long testId, @RequestAttribute("userId") Long userId) {
        testServer.reOpen(testId, userId);
    }

    @PutMapping(value = "/dispatch/{id}")
    public void dispatch(@PathVariable(value = "id")Long testId, @RequestAttribute("userId") Long userId) {
        testServer.dispatch(testId, userId);
    }

    @PutMapping(value = "/submit/{id}")
    public void submit(@PathVariable(value = "id") Long testId, @RequestBody CodeSubmitRecordDTO csrDTO, @RequestAttribute("userId") Long userId) {
        testServer.submit(testId, userId, csrDTO);
    }

    @PutMapping(value = "/close/{id}")
    public void close(@PathVariable(value = "id")Long testId, @RequestAttribute("userId") Long userId) {
        testServer.close(testId, userId);
    }

    @PutMapping(value = "/close2")
    public void close(@Validated({GroupUpdate.class}) @RequestBody TestDTO testDTO, @RequestAttribute("userId") Long userId) {
        testServer.close2(testDTO, userId);
    }

}
